<?php
/**
 * Created by 线路类.
 * User: 尚金殿
 * Date: 2018/8/22
 * Time: 16:09
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class LineController extends CommonController
{
    protected $pk = 'id';
    protected $mysql = 'line';
    protected $name = 'title';
    protected $status = 'status';

    /**
     * @return mixed|void
     *
     */
    public function lists()
    {
        $keyWord = $this->request->param('key_word');

        //关键词
        if(!empty($keyWord)){
            $res = Db::name($this->mysql)->where($this->name,'like',$keyWord.'%')->where($this->status,'>',-1)->where('class_id','<',3)->paginate(10);
        }else if(!empty($classId)){
            //分类
            $res = Db::name($this->mysql)->where('class_id',$classId)->where($this->status,'>',-1)->where('class_id','<',3)->paginate(10);
        }else{
            $res = Db::name($this->mysql)->where($this->status,'>',-1)->where('class_id','>',2)->paginate(10);
        }

        //变量输出
        $this->assign(['key_word'=>$keyWord,'res'=>$res]);

        //加载模版
        return $this->fetch();
    }
    //商品添加
    public function add(){
        if(Request::instance()->isPost()){
            //获取提交的数据
            $data = $this->request->param();
        
            //判定数据
            if(!$data['ware_type_id'] || !is_numeric($data['ware_type_id'])){
                return show(0,'商品分类不能为空');
            }else if($data['ware_type_id'] == 2){
                //秒杀
                if(!$data['seckill_price']){
                    return show(0,'秒杀价格不能为空');
                }
                if(!$data['seckill_start_time']){
                    return show(0,'秒杀开始时间不能为空');
                }
                if(!$data['seckill_end_time']){
                    return show(0,'秒杀结束时间不能为空');
                }
                if(!$data['seckill_num']){
                    return show(0,'秒杀数量不能为空');
                }
            }else if($data['ware_type_id'] == 3){
                if(!$data['collage_price']){
                    return show(0,'团购价格不能为空');
                }
                if(!$data['collage_people']){
                    return show(0,'团购人数不能为空');
                }
            }
//            if(!isset($data['config'])){
//                return show(0,'商品属性不能为空');
//            }
            if(!$data['price']){
                return show(0,'价格不能为空');
            }
//            if(!$data['city']){
//                return show(0,'出发城市不能为空');
//            }

            if(!empty($data['id'])){
                $this->save($data);
            }

            //将数组转化成字符串
            $data['config'] = implode(',',$data['config']);
            if(isset($data['img'])){
                $slider['img'] = implode(',',$data['img']);
                unset($data['img']);
            }


            //入库
            $lineId = $this->getAdd($data);

            if($lineId){
                $slider['line_id'] = $lineId;
                $slider['status'] = 1;
                $res = Db::name('slider')->insertGetId($slider);
                if(!$res){
                    return show(0,'新增失败');
                }
                    return show(1,'新增成功');
            }else{
                return show(0,'新增失败');
            }

        }else{
            //获取商品分类
            $wareType = config('WareType');
            //获取商品属性
            $config = Db::name('line_config')->where('status','>',-1)->select();

            //变量输出
            $this->assign(['wareType'=>$wareType,'config'=>$config]);

            //加载模版
            return $this->fetch();
        }
    }


    //编辑操作
    public function edit(){
        //获取主键ID
        $id = $this->request->param('id');
        //查找数据
        $ware = Db::name($this->mysql)->where($this->pk,$id)->find();
        $img = Db::name('slider')->where('line_id',$id)->find();
        $ware['img'] = $img['img'];
        $ware['config'] = explode(',',$ware['config']);

        //获取商品分类
        $wareType = config('WareType');
        //获取商品属性
        $config = Db::name('line_config')->where('status','=',1)->select();

        //变量输出
        $this->assign(['ware'=>$ware,'wareType'=>$wareType,'config'=>$config]);

        //加载模版
        return $this->fetch();
    }

    public function save($data){
        if(!$data || !is_array($data)){
            return show(0,'传递的数据不合法');
        }

        $id = $data[$this->pk];
        unset($data[$this->pk]);


        //清空其他数据
        if($data['ware_type_id'] == 1){
            //团购数据清空
            $data['collage_price'] = 0;
            $data['collage_num'] = 0;
            $data['collage_time'] = 0;
            //秒杀价格清空
            $data['seckill_price'] = 0;
            $data['seckill_start_time'] = 0;
            $data['seckill_end_time'] = 0;
            $data['seckill_num'] = 0;
        }else if($data['ware_type_id'] == 2){
            //团购数据清空
            $data['collage_price'] = 0;
            $data['collage_num'] = 0;
            $data['collage_time'] = 0;
        }else{
            //秒杀价格清空
            $data['seckill_price'] = 0;
            $data['seckill_start_time'] = 0;
            $data['seckill_end_time'] = 0;
            $data['seckill_num'] = 0;
        }


        $data['update_time'] = time();
        //修改轮播库数据
        $data['config'] = implode(',',$data['config']);

        //判定有么有轮播
        if(isset($data['img'])){
            $slider['img'] = implode(',',$data['img']);
            unset($data['img']);
        }


        //修改线路主库数据
        $res = Db::name($this->mysql)->where($this->pk,$id)->update($data);


        if($res){
            Db::name('slider')->where('line_id',$id)->update($slider);

            return show(1,'更新成功');
        }

    }



}